package cn.com.gaobb.service.impl;

import cn.com.gaobb.dto.ResultDTO;
import cn.com.gaobb.entity.TGoods;
import cn.com.gaobb.mapper.TGoodsMapper;
import cn.com.gaobb.service.GoodsService;
import cn.com.gaobb.utils.BeanHelper;
import cn.com.gaobb.utils.IdBuilderUtil;
import cn.com.gaobb.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private TGoodsMapper goodsMapper;
    @Override
    public ResultDTO<ResIdVO> add(ReqGoodsAddVO reqVO) {
        TGoods tGoods = BeanHelper.copyProperties(reqVO, TGoods.class);
        tGoods.setId(IdBuilderUtil.getId());
        tGoods.setCreateTime(new Date());
        tGoods.setModifyTime(new Date());
        goodsMapper.insertSelective(tGoods);
        return ResultDTO.okOf(new ResIdVO(tGoods.getId()));
    }

    @Override
    public ResultDTO update(ReqGoodsUpdateVO vo) {
        TGoods tGoods = BeanHelper.copyProperties(vo, TGoods.class);
        tGoods.setModifyTime(new Date());
        goodsMapper.updateByPrimaryKeySelective(tGoods);
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO deleteList(ReqGoodsDeleteListVO vo) {
        if(CollectionUtils.isEmpty(vo.getIdList())){
            return ResultDTO.errorOf(1001, "请选择要删除的客户");
        }
        vo.getIdList().forEach(id->{
            goodsMapper.deleteByPrimaryKey(id);
        });
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO<ResGoodsDetailVO> detail(String id) {
        TGoods tGoods = goodsMapper.selectByPrimaryKey(id);
        ResGoodsDetailVO resGoodsDetailVO = BeanHelper.copyProperties(tGoods, ResGoodsDetailVO.class);
        return ResultDTO.okOf(resGoodsDetailVO);
    }

    @Override
    public ResultDTO<ResPageVO<ResGoodsDetailVO>> listOfPage(ReqPageVO vo) {
        PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        List<ResGoodsDetailVO> list =  goodsMapper.list(vo.getSearchParem());
        PageInfo<ResGoodsDetailVO> pageInfo = new PageInfo<>(list);
        ResPageVO<ResGoodsDetailVO> resPageVO = new ResPageVO<>();
        resPageVO.setRows(pageInfo.getList());
        resPageVO.setCurrentPage(pageInfo.getPageNum());
        resPageVO.setTotal((int) pageInfo.getTotal());
        return ResultDTO.okOf(resPageVO);
    }

    @Override
    public ResultDTO<List<ResGoodsDetailVO>> list() {
        List<TGoods> tGoods = goodsMapper.selectAll();
        if(CollectionUtils.isEmpty(tGoods)){
            return ResultDTO.okOf(new ArrayList<ResGoodsDetailVO>());
        }
        List<ResGoodsDetailVO> resGoodsDetailVOS = BeanHelper.copyWithCollection(tGoods, ResGoodsDetailVO.class);
        return ResultDTO.okOf(resGoodsDetailVOS);
    }
}
